﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="DatabaseOptions.cs" company="SmartPlan">
//   SmartPlan 2011
// </copyright>
// <summary>
//   Defines the DatabaseOptions type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace DbDiff.Common.DataAccess
{
    using System.Data;
    using System.Data.SqlClient;
    using Helper;
    using Model;

    /// <summary>
    /// The database options.
    /// </summary>
    public class DatabaseOptions : DatabaseInfoExtended
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="DatabaseOptions"/> class.
        /// </summary>
        /// <param name="databaseObjectTypes">
        /// The dataset name.
        /// </param>
        /// <param name="databaseInfoBase">
        /// The database infobase obj.
        /// </param>
        public DatabaseOptions(DatabaseObjectTypes databaseObjectTypes, DatabaseInfoBase databaseInfoBase) : base(databaseObjectTypes, databaseInfoBase)
        {
        }

        /// <summary>
        /// The fill data set.
        /// </summary>
        /// <param name="schema">
        /// The schema.
        /// </param>
        /// <param name="table">
        /// The table.
        /// </param>
        public override void FillDataSet(string schema, string table)
        {
            using (var connection = new SqlConnection(this.DatabaseInfoBase.ConnectionString))
            {
                using (var dataAdapter = new SqlDataAdapter())
                {
                    dataAdapter.SelectCommand = new SqlCommand(this.SqlCommandString(), connection);
                    dataAdapter.SelectCommand.Parameters.Add("@ProcSchema", SqlDbType.NVarChar, 400, "ProcSchema")
                        .Value = schema;
                    dataAdapter.SelectCommand.Parameters.Add("@Name", SqlDbType.NVarChar, 400, "Name").Value =
                        table;
                    dataAdapter.SelectCommand.CommandTimeout = this.QueryTimeOut;
                    connection.Open();
                    dataAdapter.MissingMappingAction = MissingMappingAction.Passthrough;
                    dataAdapter.MissingSchemaAction = MissingSchemaAction.Add;
                    dataAdapter.SelectCommand.CommandTimeout = this.QueryTimeOut;
                    dataAdapter.Fill(this.DatabaseInfoBase.DatabaseDataSets.Tables[DatabaseObjectDictionary.ObjectTypes[this.DatabaseObjectTypes]]);
                }

                connection.Close();
            }
        }
    }
}